//
//  MediaModel.m
//  GC_OC
//
//  Created by Macbook Pro on 2023/4/24.
//

#import "MediaModel.h"

@implementation MediaModel

- (instancetype)init
{
    if (self = [super init]) {
        self.cellH = fk_adjustW(60);
        self.titleFont = fk_adjustFont(18);
    }
    return self;
}

+ (NSArray *)mj_ignoredPropertyNames
{
    return @[@"tags", @"tagFrames", @"cellH", @"titleFont"];
}

- (void)mj_didConvertToObjectWithKeyValues:(NSDictionary *)keyValues
{
    NSInteger hours = 0;
    NSInteger minutes = 0;
    NSInteger seconds = 0;
    if (_audio_num > 3600) { // 大于1小时【显示：小时-分钟-秒】
        hours = _audio_num / 3600;
        minutes = _audio_num % 3600 / 60;
        seconds = _audio_num % 60;
        _audioDuration = fkFORMAT(@"%02zd:%02zd:%02zd", hours, minutes, seconds);
    } else if (_audio_num > 60) { // 大于1分钟【显示分钟和秒】
        minutes = _audio_num / 60;
        seconds = _audio_num % 60;
        _audioDuration = fkFORMAT(@"%02zd:%02zd", minutes, seconds);
    } else { // 只显示秒
        seconds = _audio_num;
        _audioDuration = fkFORMAT(@"00:%02zd", seconds);
    }
    
    if (_video_num > 3600) { // 大于1小时【显示：小时-分钟-秒】
        hours = _video_num / 3600;
        minutes = _video_num % 3600 / 60;
        seconds = _video_num % 60;
        _videoDuration = fkFORMAT(@"%02zd:%02zd:%02zd", hours, minutes, seconds);
    } else if (_video_num > 60) { // 大于1分钟【显示分钟和秒】
        minutes = _video_num / 60;
        seconds = _video_num % 60;
        _videoDuration = fkFORMAT(@"%02zd:%02zd", minutes, seconds);
    } else { // 只显示秒
        seconds = _video_num;
        _videoDuration = fkFORMAT(@"00:%02zd", seconds);
    }
    
    if (_lable_name.length > 0) {
        [self setupLabels:_lable_name];
    }
    
    if (_label.length > 0) {
        [self setupLabels:_label];
    }
    
    if (_name.length > 0) {
        CGFloat maxW = fkScreenW - fk_adjustW(30 + 80);
        CGFloat titleW = [_name sizeWithAttributes:@{NSFontAttributeName : _titleFont}].width;
        if (titleW > maxW) _titleFont = fk_adjustFont(14);
    }
    
    _top_parent_pid_name = @""; // 后台没有返回对应的值
    _top_pid_name = _media_class_pid_name ? : @"";
}

- (void)setupLabels:(NSString *)str
{
    _tags = [str componentsSeparatedByString:@";"];
//        _cellH = fk_adjustW(60);
    
    CGFloat tagX = fk_adjustW(30);
    CGFloat tagY = fk_adjustW(35);
    CGFloat tagH = fk_adjustW(16);
    CGFloat padding = fk_adjustW(4);
    NSMutableArray *tagFrames = [NSMutableArray array];
    _tagFrames = tagFrames;
    for (NSString *tag in _tags) {
//            NSInteger index = [_tags indexOfObject:tag];
        CGFloat tagW = [tag sizeWithAttributes:@{NSFontAttributeName : fk_adjustFont(10)}].width + padding * 2;
        CGRect tagFrame = CGRectMake(tagX, tagY, tagW, tagH);
        [tagFrames addObject:[NSValue valueWithCGRect:tagFrame]];
        _cellH = MAX(fk_adjustW(60), (tagY + tagH + 12));
        
        tagX = CGRectGetMaxX(tagFrame) + padding;
        if (tagX > (fkScreenW - 15 - fk_adjustW(110))) {
            tagX = 15;
            tagY += (tagH + padding);
        }
    }
}

@end
